package com.hotel.controller;

import com.hotel.pojo.Home;
import com.hotel.pojo.Rooms;
import com.hotel.pojo.Yu;
import com.hotel.pojo.Yuding;
import com.hotel.service.*;
import com.hotel.utils.SimpleIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/yu")
public class YuController {

    @Autowired
    YuServiceImpl yuService;
    @Autowired
    YudingServiceImpl yudingService;
    @Autowired
    HomeServiceImpl roomService;

    @RequestMapping("/add")
    public String add(Yuding yu){
        /*SimpleIdGenerator idGenerator = new SimpleIdGenerator(1); // 假设机器ID为1
        long l = idGenerator.nextId();*/

        Home home = new Home();
        home.setId(Integer.parseInt(yu.getRoomNumber()));
        home.setState(yu.getReservationStatus());
        //修改房间状态
        int i1 = roomService.updateH_TypeById(home);
        //查询房间得价格添加到预定信息中
        Home home1 = roomService.queryHomeById(Integer.parseInt(yu.getRoomNumber()));
        try {
            BigDecimal bigDecimal = new BigDecimal(home1.getPrice());
            yu.setTotalAmount(bigDecimal);
        } catch (NumberFormatException e) {
            // 处理错误，例如记录日志或通知用户输入无效
        }
        yu.setReservationDate(new Date());
        //插入预定信息
        int i = yudingService.addYuding(yu);
        if (i>0&&i1>0){
            return "succ";
        }else {
            return "er";
        }

    }

    @RequestMapping("/delete")
    public String delete(int id){
        yuService.deleteVipById(id);
        return "redirect:/vip/list";
    }

    @RequestMapping(value = "/list",produces = "application/json;charset=UTF-8")
    public ModelAndView list(){
        ModelAndView mv = new ModelAndView();
        ArrayList<Home> rooms = roomService.queryAllHomebystate("空房");
        mv.addObject("list",rooms);
        mv.setViewName("yu");
        return mv;
    }

    @RequestMapping("/update1")
    public ModelAndView update1(int id){
        ModelAndView mv = new ModelAndView();
        Yu yu = yuService.queryVipById(id);
        mv.addObject("v",yu);
        mv.setViewName("user_update");
        return mv;
    }

    @RequestMapping("/update2")
    public String update2(Yu yu){
        yuService.updateVipById(yu);
        return ("redirect:/vip/list");
    }

    @RequestMapping("/find")
    public ModelAndView find(String findByPhone ){
        ModelAndView mv = new ModelAndView();
        Yu yu = yuService.queryVipByPhone(findByPhone);
        List<Yu> vipList=new ArrayList<Yu>();
        vipList.add(yu);
        if (yu==null){
            vipList=yuService.queryAllVip();
            mv.addObject("error","未查询出结果");
        }
        mv.addObject("list",vipList);
        mv.setViewName("user_list");
        return mv;
    }
}

